package com.cy.pj.sys.service;

import com.cy.pj.common.annotation.ClearCache;
import com.cy.pj.common.annotation.RequiredCache;
import com.cy.pj.common.exception.ServiceException;
import com.cy.pj.common.vo.Node;
import com.cy.pj.sys.entity.SysDept;
import com.cy.pj.sys.mapper.SysDeptMapper;
import com.cy.pj.sys.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

@Service
public class SysDeptServiceImpl implements SysDeptService{
    @Autowired
    private SysDeptMapper sysDeptMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    private List<Integer> sortList;

    //@RequiredCache(key = "deptData") 开启缓存
    @Override
    public List<Map<String, Object>> findObjects() {

        List<Map<String, Object>> list = sysDeptMapper.findObjects();
        if(list == null || list.size() == 0)
            throw new ServiceException("没有部门信息");
        return list;
    }

    //返回ZTree数结构
    @Override
    public List<Node> findZTreeNodes() {
        List<Node> nodes = sysDeptMapper.findZTreeNodes();
        if(nodes == null || nodes.size() == 0)
            throw new ServiceException("没有部门信息");
        return nodes;
    }

    //修改部门信息
    //@ClearCache(key = "deptData") //更新时清除一下缓存
    @Override
    @Transactional
    public int updateObject(SysDept entity) {
        //1.合法性验证
        if(entity == null)
            throw new IllegalArgumentException("保存对象不能为空");
        if(StringUtils.isEmpty(entity.getName()))
            throw new IllegalArgumentException("部门不能为空");
        //2.判断是否有更改
        if(entity.equals(sysDeptMapper.findById(entity.getId()))){
            throw new ServiceException("未更改,不用保存");
        }
        //3.根据id查询原来的序号
        Integer sort = sysDeptMapper.findById(entity.getId()).getSort();
        //4.判断原来的id和新id是否一致
        if(!sort.equals(entity.getSort())){
            if(findSort(entity.getSort())){
                throw new IllegalArgumentException("序号已经存在,请重新输入");
            }
        }
        //5.判断上级部门是否正确
        Integer parentId = sysDeptMapper.findById(entity.getId()).getId();
        if(parentId.equals(entity.getParentId())){
            throw new IllegalArgumentException("上级部门不能是自己");
        }
        int rows;
        try {
            rows = sysDeptMapper.updateObject(entity);
        }catch (Exception e){
            e.printStackTrace();
            throw new ServiceException("更新失败");
        }
        return rows;
    }

    //@ClearCache(key = "deptData") //更新时清除一下缓存
    @Override
    @Transactional
    public int saveObject(SysDept entity) {
        //1.合法性验证
        if(entity == null)
            throw new IllegalArgumentException("保存对象不能为空");
        if(StringUtils.isEmpty(entity.getName()))
            throw new IllegalArgumentException("部门不能为空");
        //2.判断部门序号是否已存在
        if(findSort(entity.getSort())){
            throw new ServiceException("该序号已经存在,请重新输入序号");
        }
        //3.保存数据
        int rows = sysDeptMapper.insertObject(entity);
        if(rows == 0)
            throw new ServiceException("保存失败");
        return rows;
    }

    //删除部门信息--如果部门下有子部门,或者部门中还有员工,则不允许删除
    //@ClearCache(key = "deptData") //更新时清除一下缓存
    @Override
    public int deleteObject(Integer id) {
        //1.合法性校验
        if(id == null)
            throw new IllegalArgumentException("请选择一个部门");
        //2.判断是否有子部门
        int children = sysDeptMapper.getChildren(id);
        if(children > 0)
            throw new ServiceException("请先删除子部门");
        //3判断该部门中是否还存在员工,有员工则不允许删除
        int users = sysUserMapper.getUserCountByDeptId(id);
        if(users > 0)
            throw new ServiceException("此部门中还有员工,不允许删除部门");
        //4.删除部门
        int rows = sysDeptMapper.deleteObject(id);
        if(rows == 0)
            throw new ServiceException("该部门可能已经被删除");
        return rows;
    }



    //查询部门序号是否存在
    @Override
    public boolean findSort(Integer sort) {
        //1.判断部门序号集合是存在
        sortList = sysDeptMapper.findSort();
        //2.如果存在,判断是否有元素
        if(sortList.size() == 0){
            return false;
        }else {
            //3.判断集合中是否存在当前序号
            return sortList.contains(sort);
        }
    }
}
